#include "anmation.h"
#include<gamedefine.h>
#include<player.h>
void anmation::start(int intervalMs)
{
    if (m_frames.isEmpty()) return;
    idex = 0;
    animationtimer->start(intervalMs);
}

void anmation::stop()
{
    animationtimer->stop();
    idex=0;

}

void anmation::setLoop(bool loop)
{
    m_isLooping=loop;
}

void anmation::updateFrame()
{
    if (idex >= m_frames.size()) {
        if (m_isLooping) {
            idex = 0; // 循环播放
        } else {
            stop();
            emit animationFinished(); // 通知动画结束
            return;
        }
    }

    // 发送当前帧
    emit frameChanged(m_frames[idex]);
    idex++;
}

anmation::~anmation()
{
    animationtimer-> stop();
    disconnect();

}

anmation::anmation() {}

anmation::anmation(QObject*parent,QString situation):QObject(parent)
{   animationtimer = new QTimer(this);
    connect(animationtimer, &QTimer::timeout, this, &anmation::updateFrame);
    m_frames.clear();
    if(situation=="idle"){
        QPixmap run (":/gameresource/Idle (32x32).png");
        for(int i=0;i<11;i++){
            QPixmap frame = run.copy(i * 32, 0, 32, 32);
            m_frames.append(frame);

        }


    }
    else if(situation=="jump"){
        QPixmap jump (":/gameresource/Jump (32x32).png");
        for(int i=0;i<1;i++){
            QPixmap frame = jump.copy(i * 32, 0, 32, 32);
            m_frames.append(frame);


        }

    }
    else if(situation=="doublejump"){
        QPixmap doublejump (":/gameresource/Double Jump (32x32).png");

        for(int i=0;i<6;i++){
            QPixmap frame = doublejump.copy(i * 32, 0, 32, 32);
            m_frames.append(frame);


        }
        QPixmap fall(":/gameresource/Fall (32x32).png");
        m_frames.append(fall);

    }
    else if(situation=="run"){
        QPixmap run (":/gameresource/Run (32x32).png");

        for(int i=0;i<12;i++){
            QPixmap frame = run.copy(i * 32, 0, 32, 32);
            m_frames.append(frame);


        }

    }
    else if(situation=="hit"){
        QPixmap hit (":/gameresource/Hit (32x32).png");

        for(int i=0;i<14;i++){
            QPixmap frame = hit.copy(i/2 * 32, 0, 32, 32);
            m_frames.append(frame);


        }

    }
    else if(situation=="fall"){
        QPixmap fall (":/gameresource/Fall (32x32).png");

        for(int i=0;i<1;i++){
            QPixmap frame = fall.copy(i * 32, 0, 32, 32);
            m_frames.append(frame);

        }

    }
    else if(situation=="leftrun"){
        QPixmap run (":/gameresource/Run (32x32).png");
        QVector<QPixmap> runRightFrames;
        for(int i=0;i<12;i++){
            QPixmap frame = run.copy(i * 32, 0, 32, 32);
           runRightFrames.append(frame);

        }
        for (const QPixmap& frame : runRightFrames) {
            // 水平翻转每一帧
            QImage image = frame.toImage();
            QTransform transform;
            transform.scale(-1, 1); // 水平翻转
            transform.translate(-image.width(), 0); // 修正偏移
            QImage flippedImage = image.transformed(transform);
            m_frames.append(QPixmap::fromImage(flippedImage));
        }

    }
    else if(situation=="apple"){
        QPixmap apple(":/gameresource/Apple.png");
        for(int i=0;i<17;i++){
            QPixmap frame = apple.copy(i * 32, 0, 32, 32);
            m_frames.append(frame);

        }


    }

}


